package edu.cuny.baruch.cis9590.library.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.apache.commons.codec.digest.DigestUtils;

import edu.cuny.baruch.cis9590.library.database.LoginDAO;
import edu.cuny.baruch.cis9590.library.entities.Login;

final class LoginPanel extends LibraryPanel {
    
    private final LibraryGUI libraryGUI;
    private final LoginDAO loginDAO;
    private final JTextField usernameField;
    private final JPasswordField passwordField;
    
    public LoginPanel(LibraryGUI libraryGUI) {
        this.libraryGUI = libraryGUI;
        loginDAO = new LoginDAO();
        ActionListener loginActionListener = new LoginActionListener();
        
        GridBagLayout gridBagLayout = new GridBagLayout();
        setLayout(gridBagLayout);
        
        JLabel usernameLabel = new JLabel("Username");
        usernameLabel.setDisplayedMnemonic('U');
        GridBagConstraints gbc_usernameLabel = new GridBagConstraints();
        gbc_usernameLabel.insets = new Insets(0, 0, 5, 5);
        gbc_usernameLabel.anchor = GridBagConstraints.WEST;
        gbc_usernameLabel.gridx = 0;
        gbc_usernameLabel.gridy = 0;
        add(usernameLabel, gbc_usernameLabel);
        
        usernameField = new JTextField();
        usernameField.setColumns(20);
        GridBagConstraints gbc_usernameField = new GridBagConstraints();
        gbc_usernameField.insets = new Insets(0, 0, 5, 0);
        gbc_usernameField.fill = GridBagConstraints.HORIZONTAL;
        gbc_usernameField.gridx = 1;
        gbc_usernameField.gridy = 0;
        add(usernameField, gbc_usernameField);
        usernameField.addActionListener(loginActionListener);
        usernameLabel.setLabelFor(usernameField);
        
        JLabel passwordLabel = new JLabel("Password");
        passwordLabel.setDisplayedMnemonic('P');
        GridBagConstraints gbc_passwordLabel = new GridBagConstraints();
        gbc_passwordLabel.insets = new Insets(0, 0, 5, 5);
        gbc_passwordLabel.anchor = GridBagConstraints.WEST;
        gbc_passwordLabel.gridx = 0;
        gbc_passwordLabel.gridy = 1;
        add(passwordLabel, gbc_passwordLabel);
        
        passwordField = new JPasswordField();
        passwordField.setColumns(20);
        GridBagConstraints gbc_passwordField = new GridBagConstraints();
        gbc_passwordField.insets = new Insets(0, 0, 5, 0);
        gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
        gbc_passwordField.gridx = 1;
        gbc_passwordField.gridy = 1;
        add(passwordField, gbc_passwordField);
        passwordField.addActionListener(loginActionListener);
        passwordLabel.setLabelFor(passwordField);
        
        JButton registerButton = new JButton("Register");
        GridBagConstraints gbc_registerButton = new GridBagConstraints();
        gbc_registerButton.fill = GridBagConstraints.HORIZONTAL;
        gbc_registerButton.insets = new Insets(0, 0, 5, 0);
        gbc_registerButton.gridx = 1;
        gbc_registerButton.gridy = 3;
        add(registerButton, gbc_registerButton);
        registerButton.addActionListener(new RegisterActionListener());
        
        final JButton loginButton = new JButton("Login");
        GridBagConstraints gbc_loginButton = new GridBagConstraints();
        gbc_loginButton.insets = new Insets(0, 0, 5, 0);
        gbc_loginButton.fill = GridBagConstraints.HORIZONTAL;
        gbc_loginButton.gridx = 1;
        gbc_loginButton.gridy = 2;
        add(loginButton, gbc_loginButton);
        loginButton.addActionListener(loginActionListener);
    }
    
    private final class LoginActionListener implements ActionListener {
        @Override public void actionPerformed(ActionEvent e) {
            String username = usernameField.getText();
            
            Login login = loginDAO.getLogin(username);
            if (login == null) {
                JOptionPane.showMessageDialog(LoginPanel.this, "Invalid username");
                return;
            }
            
            String hashedInputPassword = DigestUtils.shaHex(new String(passwordField.getPassword()));
            boolean passwordMatches = login.getHashedPassword().equals(hashedInputPassword);
            
            if (!passwordMatches) {
                JOptionPane.showMessageDialog(LoginPanel.this, "Invalid password");
                return;
            }
            
            usernameField.setText("");
            passwordField.setText("");
            login.setLastLogin(new Date());
            loginDAO.update(login);
            libraryGUI.login(login);
        }
    }
    
    private final class RegisterActionListener implements ActionListener {
        @Override public void actionPerformed(ActionEvent e) {
            libraryGUI.changePanel(LibraryGUI.Panel.REGISTER);
        }
    }
    
    @Override
    public void clear() {
        usernameField.setText("");
        passwordField.setText("");
    }
}
